<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class AwardGoods extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_award_goods';
    protected $cacheModel = 'award.award_goods';

    /**
     * 添加赠送记录
     * @param $data
     * @return bool
     */
    public function saveData($data)
    {
        try
        {
            $this->insert($data);
        }
        catch (\Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 缓存获取赠送信息
     * @param $uid
     * @param $order_type 1=>幸运之城,2=>教学视频，3=>优惠券
     * @throws \Exception
     */
    public function getUserAwardFromCache($uid, $order_type)
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setListKeyType($order_type)->ZsGetIocData('list', function () use ($uid, $order_type)
        {
            return $this->getUserAwardFromDms($uid, $order_type);
        });
        return $this->batchGetInfoByIds($ids);
    }

    /**
     * 数据库获取赠送信息
     * @param $uid
     * @param $order_type
     * @return array|null
     */
    public function getUserAwardFromDms($uid, $order_type)
    {
        try
        {
            $data = $this->where(['auid' => $uid, 'type' => $order_type])->column('id', 'id');
            $data = array_flip_key_value($data);
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    /**
     * 批量获取赠送信息
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $field = 'status,type,auid uid,agid gid';
        try
        {
            $data = $this->where(['id' => ['in', $ids]])->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }
}
